﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using Castle.ActiveRecord;
using System.Web.UI;
using System.IO;
using SimplePMT.Data;
using SimplePMT.Components;
using NHibernate.Expression;

namespace SimplePMT.Control
{
    public partial class TimeReportingControl : System.Web.UI.UserControl
    {
        public string FromDate = string.Empty;
        public string ToDate = string.Empty;
        public bool ShowSummary = false;
        public string TimeReportContents = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        public override string ToString()
        {
            if (ShowSummary)
            {
                TimeReportContents = GetTimeReportSummary(FromDate, ToDate);
            }
            else
            {
                TimeReportContents = GetTimeReport(FromDate, ToDate);
            }
            string result = "";
            StringWriter writer = new StringWriter();
            HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
            this.Render(htmlWriter);
            result = writer.ToString();
            return result;
        }

        private string GetTimeReport(string FromDate, string ToDate)
        {
            StringBuilder sb = new StringBuilder();
            Member[] members = Member.FindAll(Expression.Eq("ReadOnly", false), Expression.Eq("Terminated", false));

            foreach (Member member in members)
            {
                StringBuilder ma = new StringBuilder();

                decimal totalHours = 0;
                string project = "";
                string taskTitle = "";
                ma.Append("<tr><td colspan=2><table width=100% style=\"border:1px solid #000000\">");

                TaskNote[] notes = TaskNote.FindAll(Order.Asc("DateTime"), Expression.Eq("Member", member), Expression.Ge("DateTime", Convert.ToDateTime(FromDate)), Expression.Le("DateTime", Convert.ToDateTime(ToDate)));
                decimal hrs = 0;
                foreach (TaskNote note in notes)
                {
                    hrs = note.Hours;
                    taskTitle = note.Task.Title;
                    int taskContainerItemID = 0;
                    TaskContainerItem[] tci = TaskContainerItem.FindAll(Expression.Eq("TaskID", note.Task.ID));
                    if (tci.Length > 0)
                    {
                        taskContainerItemID = tci[0].TaskContainer.ID;
                        TaskContainer pjContainer = TaskContainer.Find(Common.GetRecursiveTaskTreeID(tci[0].TaskContainer));
                        project = pjContainer.Label;
                        if (pjContainer.IncludeInProjectPlan)
                        {
                            totalHours += hrs;
                            ma.Append("<tr onmouseover=\"this.className='highlightRow';\" onmouseout=\"this.className='';\" OnClick=\"openModal('TaskView.aspx?id=" + note.Task.ID + "&cid=" + taskContainerItemID + "', 'SimplePMT - " + note.Task.Title.Replace("'", "\\'").Replace("\"", "") + "');\">");
                            ma.Append("<td width=70% style=\"font-size:11px\">&nbsp;<b>" + project + "</b> : " + taskTitle + "</td><td  width=20% align=right style=\"font-size:11px\">" + note.DateTime.ToShortDateString() + "</td><td align=right style=\"font-size:11px\">" + note.Hours + " hrs</td>");
                            ma.Append("</tr>");
                        }
                    }
                }

                ma.Append("</td></tr></table>");


                sb.Append("&nbsp;<br><table width= 100%><tr><td><span style=\"font-size:14px;\"><b>" + member.FirstName + " " + member.LastName + "</b></span></td><td align=right >&nbsp;&nbsp;Total:<b>" + totalHours + "</b></td></tr>" + ma.ToString() + "</table>");

            }
            return sb.ToString();
        }
        private string GetTimeReportSummary(string FromDate, string ToDate)
        {
            int daySpan = 0;
            DateTime EndDate = Convert.ToDateTime(ToDate);
            for (int m = 0; m < 5000; m++)
            {
                if (Convert.ToDateTime(FromDate).AddDays(m).Equals(EndDate))
                {
                    daySpan = m;
                    break;
                }
            }

            StringBuilder sb = new StringBuilder();
            Member[] members = Member.FindAll(Expression.Eq("ReadOnly", false), Expression.Eq("Terminated", false));

            foreach (Member member in members)
            {
                StringBuilder ma = new StringBuilder();
                decimal totalHours = 0;
                ma.Append("<tr><td colspan=2><table width=100% style=\"border:1px solid #000000\">");


                for (int i = 0; i <= daySpan; i++)
                {
                    TaskNote[] notes = TaskNote.FindAll(Expression.Eq("Member", member), Expression.Gt("DateTime", Convert.ToDateTime(FromDate +" 23:59:59").AddDays((i - 1))), Expression.Lt("DateTime", Convert.ToDateTime(FromDate +" 00:00:00").AddDays((i + 1))));
                    int noteCount = 0;
                    decimal hrs = 0;
                    foreach (TaskNote note in notes)
                    {
                        noteCount++;
                        hrs += note.Hours;
                    }
                    ma.Append("<tr>");
                    ma.Append("<td width=70% style=\"font-size:11px\" align=right>Summary: total of "+ noteCount +" task note entries on <b>"+ Convert.ToDateTime(FromDate).AddDays(i).ToShortDateString() +"</b>&nbsp;</td><td align=right style=\"font-size:11px\">" + hrs + " hrs</td>");
                    ma.Append("</tr>");
                    totalHours += hrs;
                }

                ma.Append("</table></td></tr>");


                sb.Append("&nbsp;<br><table width= 100%><tr><td><span style=\"font-size:14px;\"><b>" + member.FirstName + " " + member.LastName + "</b></span></td><td align=right >&nbsp;&nbsp;Total:<b>" + totalHours + "</b></td></tr>" + ma.ToString() + "</table>");

            }
            return sb.ToString();
        }

        
        
    }
    
}